<?php

Class Land {

	private $record, $record_orig, $records, $data;
	public $db, $_test;
	
	function __construct() {
		$this->_test = new Test();
	}
	
/**
* Returns a since field value
* @return mixed
*/		
	public function get( $field ) {
				
		if (isset($this->record[$field])) {
			return( $this->record[$field] );
		} else {
			return( false );
		}
	}

/**
* Set the value to a field
* @return bool
*/		
	public function set( $field, $value ) {
	
		$this->record[$field] = $value;
		return( true );
		
	}

/**
* Sets the filtered data to the record array
* @param mixed : $data : array of filtered inputs
* @return bool
*/

	public function setData($data) {

		if(count($data)) {
			foreach($data as $key => $value) {
				$this->record[$key] = $value;
			}
		}
		return( true );
	}
 
/**
* Returns all values for project
* @return array
*/		
	public function get_all( $set = 'NEW' ) {

		if ($set == 'NEW') {
			return( $this->record );
		}

		if ($set == 'ORIG') {
			return( $this->record_orig );
		}
		
		return( false );
		
	}

/**
* Loads the land by id
*/		
	public function load_by_id($land_id) {
		$query = sprintf("SELECT * FROM `land` WHERE `land_id` = %s ", mysql_escape_string($land_id) );
		$ret = $this->db->query_one( $query );
		unset($this->records);
		if ($ret != NULL) {
			$records = array();
			foreach( $ret as $field => $value ) {
			
				$this->record_orig[$field] = $value;
				$this->record[$field] = $value;
				
			}
			$this->records[] = $this->record;
			return(true);
		} else {
			return(false);
		}
	}

	public function save(){
		if($this->recordExists($this->get('land_id'))){
			$query = sprintf(" UPDATE `land` SET `polygon` = '%s', `title` = '%s', `description` = '%s', `private` = '%s' WHERE `land_id` = %s "
			, $this->get('polygon')
			, $this->get('title')
			, $this->get('description')
			, $this->get('private')
			, $this->get('land_id')
			);

		} else {
			$query = sprintf("INSERT INTO `land` SET `polygon` = '%s', `title` = '%s', `description` = '%s', `private` = '%s' "
			, $this->get('polygon')
			, $this->get('title')
			, $this->get('description')
			, $this->get('private')
			);
		}

		try {
			$result = $this->db->query($query);
		} catch (Exception $e) {
			trigger_error($e->getMessage(),E_USER_ERROR);
		}
		return ( $result );

	}

	/**
	*checks whether record exists in database
	*/
	public function recordExists ($land_id){

		$query = sprintf("SELECT `land_id` FROM `land` WHERE `land_id` = '%s';", mysql_escape_string($land_id) );

		$ret = $this->db->query_one( $query );
		if ($ret == NULL) {
			return false;
		} else {
			return true;
		}
	}

	public function getList (){
		$query = sprintf("SELECT l.land_id, l.%s as value FROM land l"
			, mysql_escape_string($this->get('field'))
		);

		if ($this->get('user_id') != "" && $this->get('showall') != true) {
			$query .= sprintf(" JOIN user_land ON user_land.land_id  = l.land_id AND user_land.user_id = %s "
				, mysql_escape_string($this->get('user_id'))
			);
		}
		
		#Order
		$query .= sprintf(" WHERE `%s` LIKE '%s%%' ORDER BY `%s` %s "
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('value'))
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('sort'))
		);
		
		if( $this->get('start') != "" && $this->get('limit') != "" ) {
			$where = sprintf(" LIMIT %s, %s"
				,mysql_escape_string($this->get('start'))
				,mysql_escape_string($this->get('limit'))
				);
			$query .= $where;
		}
		$ret = $this->db->query_all($query);
		
		return $ret;

	}

	public function listRecords (){
		if($this->get('filter')) {
			$where = build_where($this->get('filter'));
		}
		if ($where != '') {
			$where = " WHERE " . $where;
		}
		if($this->get('order') != '') {
			$where .= build_order( $this->get('order') );
		}
		$where .= build_limit( $this->get('start'), $this->get('limit') );

		$query = sprintf("SELECT * FROM `land` " . $where);

		try {
// 			$ret = $this->db->query_page_all( $query, $this->get('limit') , $this->get('start') );
			$ret = $this->db->query_all( $query );
		} catch (Exception $e) {
			trigger_error($e->getMessage(),E_USER_ERROR);
		}
		
		return $ret;
	}

}
?>